package com.ze.familyday.familyphotoframesquare;

import java.util.ArrayList;
import com.mouse.customview.CustomDialog;
import com.umeng.analytics.MobclickAgent;
import com.ze.commontool.Constant;
import com.ze.familydayverpm.adapter.ScanAdapter;
import com.ze.familydayverpm.adapter.ScanAdapter.CloseActivityCallBack;
import com.ze.model.PhotoModel;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class ScanActivity extends Activity implements OnClickListener{

	/**
	 * viewID
	 */
	private ListView lvPhotos;
	private BaseAdapter mAdapter;
	private ImageButton ibPlayAll;
	private ImageButton ibPlayLove;
	private ImageButton ibPlayMine;
	private Dialog mLoadingDialog;
	/**
	 * 数据结构成员
	 */
	private ArrayList<PhotoModel> mListData = new ArrayList<PhotoModel>();
	/**
	 * 用户信息
	 */
	
	private int play_model;
	/**
	 * Some Constant
	 */
	public static final int PLAYING_ALL = 22;
	public static final int PLAYING_LOVE = 11;
	public static final int PLAYING_MINE = 33;
	
	/**
	 * 如果ScanActivity被启动，则为true
	 */
	public static boolean hasStartScanAct = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);
		initData();
		initView();
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				/*mListData.addAll(list);
				mAdapter.notifyDataSetChanged();*/
				mAdapter = new ScanAdapter(ScanActivity.this, PhotoFrameActivity.mListData, callBack);
				lvPhotos.setAdapter(mAdapter);
				hasStartScanAct = true;
			}
		}, 200);
		IntentFilter filter = new IntentFilter();
		filter.addAction("to_scan_to_loadmorepic");
		registerReceiver(receiver, filter);
	}
	
	BroadcastReceiver receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals("to_scan_to_loadmorepic")) {
				if (mLoadingDialog.isShowing()) {
					mLoadingDialog.dismiss();
				}
				if (mAdapter != null) {
					mAdapter.notifyDataSetChanged();
				}
			}
		}
	};
	
	private void initData() {
		play_model = getIntent().getIntExtra("model", PLAYING_ALL);
	}
	
	private void initView() {
		lvPhotos = (ListView) findViewById(R.id.lvPhotos);
		ibPlayAll = (ImageButton) findViewById(R.id.ibPlayAll);
		ibPlayAll.setOnClickListener(this);
		ibPlayLove = (ImageButton) findViewById(R.id.ibPlayLove);
		ibPlayLove.setOnClickListener(this);
		ibPlayMine = (ImageButton) findViewById(R.id.ibPlayMine);
		ibPlayMine.setOnClickListener(this);
		if (play_model == PLAYING_MINE) {
			ibPlayMine.setSelected(true);
		}else if (play_model == PLAYING_LOVE) {
			ibPlayLove.setSelected(true);
		}else {
			ibPlayAll.setSelected(true);
		}
		mLoadingDialog = new CustomDialog.Builder(this)
		.setCustomButton(new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				mLoadingDialog.dismiss();
			}
		})
		.create(1);
		mLoadingDialog.setCanceledOnTouchOutside(false);
	}
	

	@Override
	public void onClick(View v) {
		if (v == ibPlayAll) {
			//loadListTask(true, PLAYING_ALL);
			switchToModel(PLAYING_ALL);
			setSelectState(ibPlayAll);
		}else if (v == ibPlayLove) {
			//loadListTask(true, PLAYING_LOVE);
			switchToModel(PLAYING_LOVE);
			setSelectState(ibPlayLove);
		}else if (v == ibPlayMine) {
			//loadListTask(true, PLAYING_MINE);
			switchToModel(PLAYING_MINE);
			setSelectState(ibPlayMine);
		}
	}
	
	public void switchToModel(int model) {
		//TODO:switchToModel
		play_model = model;
		mLoadingDialog.show();
		PhotoFrameActivity.mListData.clear();
		mAdapter.notifyDataSetChanged();
		Intent intent = new Intent();
		intent.setAction(Constant.ACTION.TO_PHOTO_TO_SWITCH_MODEL);
		intent.putExtra("model", model);
		sendBroadcast(intent);
	}
	
	private void setSelectState(View view) {
		ibPlayAll.setSelected(false);
		ibPlayLove.setSelected(false);
		ibPlayMine.setSelected(false);
		view.setSelected(true);
		mListData.clear();
		mAdapter.notifyDataSetChanged();
	}
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			
		}
		
	};
	
	CloseActivityCallBack callBack = new CloseActivityCallBack() {
		
		@Override
		public void closeActivity(int index) {
			Intent intent = new Intent();
			intent.setAction(Constant.ACTION.SCAN_ACTIVITY_FINISH);
			intent.putExtra("index", index);
			intent.putExtra("play_model", play_model);
			sendBroadcast(intent);
			ScanActivity.this.finish();
		}
	};
	
	@Override
	protected void onDestroy() {
		hasStartScanAct = false;
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		if (mLoadingDialog != null) {
			mLoadingDialog.dismiss();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
}
